package TestTime;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class ThreadScopeShareData {
	private static int data = 0;
	private static Map<Thread, Integer> shareData = new HashMap<Thread, Integer>();

	public static void main(String[] args) {

		for (int i = 0; i < 4; i++) {
			System.out.println("开始创建第" + i + "个线程");
			System.out.println("当前静态变量data = " + data);
			new Thread(new Runnable() {
				@Override
				public void run() {
					int data = new Random().nextInt();
					System.out.println(Thread.currentThread().getName() + " has put data : " + data);
					shareData.put(Thread.currentThread(), data);
					new A().get(shareData);
					new B().get(shareData);
				}
			}).start();
		}

	}

	static class A {
		public void get(Map<Thread, Integer> shareData) {
			int data = shareData.get(Thread.currentThread());

			System.out.println("A from " + Thread.currentThread().getName() + " has get data : " + data);
		}
	}

	static class B {
		public void get(Map<Thread, Integer> shareData) {
			int data = shareData.get(Thread.currentThread());

			System.out.println("B from " + Thread.currentThread().getName() + " has get data : " + data);
		}
	}

}
